package cn.fx.org.service.impl;

import cn.fx.basic.service.impl.BaseServiceImpl;
import cn.fx.basic.util.Md5Utils;
import cn.fx.basic.util.StrUtils;
import cn.fx.org.domain.Employee;
import cn.fx.org.mapper.EmployeeMapper;
import cn.fx.org.service.IEmployeeService;
import cn.fx.user.domain.Logininfo;
import cn.fx.user.mapper.LogininfoMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * 删除
     * @param eid
     */
    @Override
    @Transactional
    public void remove(long eid) {

        //删除登录信息表数据
        logininfoMapper.remove(employeeMapper.loadById(eid).getLogininfo_id());

        //删除t_employee:删除中间表信息：t_employee_role
        employeeMapper.removeEmployeeRole(eid);

        //删除employee表数据
        super.remove(eid);
    }

    /**
     * 添加
     * @param employee
     */
    @Override
    @Transactional
    public void save(Employee employee) {

        //1.处理密码和盐值
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);

        employee.setLogininfo_id(logininfo.getId());
        super.save(employee);

        //保存中间表的信息
        employeeMapper.saveEmployeeRole(employee);

    }

    /**
     * 修改
     * @param employee
     */
    @Override
    @Transactional
    public void update(Employee employee) {

        //1.处理密码和盐值
        //获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        //获取加密之后的密码
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword()+salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        Logininfo logininfo = employee2Logininfo(employee);
        Long logininfo_id = employeeMapper.loadById(employee.getId()).getLogininfo_id();
        logininfo.setId(logininfo_id);

        logininfoMapper.update(logininfo);
        //登录信息id和店铺信息id不能修改
        super.update(employee);

        //删除中间表信息
        employeeMapper.removeEmployeeRole(employee.getId());
        //保存新的信息
        employeeMapper.saveEmployeeRole(employee);
    }

    //批量删除
    @Override
    public void patchDel(List<Long> ids) {
        for (Long id : ids) {
            remove(id);
        }
    }

    /**
     * Logininfo对象方法
     * @param employee
     * @return
     */
    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        return logininfo;
    }

    @Override
    public List<Employee> findByShopId(Long shopId) {
        return employeeMapper.findByShopId(shopId);
    }
}
